package com.borqsprocessmanager.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;

import com.borqsprocessmanager.App;

public class FileUtil {

    /**
     * 
     * 
     * @param list
     * @param fileName
     *            "userData"
     */
    public void writeObject(LinkedList<Float> list, String fileName) {
        try {
            File file = new File(App.mContext.getFilesDir(), fileName);
            FileOutputStream outStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    outStream);

            objectOutputStream.writeObject(list);
            outStream.close();
            objectOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public LinkedList<Float> readObject(String fileName) {
        FileInputStream freader = null;
        ObjectInputStream objectInputStream = null;
        try {
            File file = new File(App.mContext.getFilesDir(), fileName);
            freader = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(freader);
            LinkedList<Float> list = new LinkedList<Float>();
            list = (LinkedList<Float>) objectInputStream.readObject();
            freader.close();
            objectInputStream.close();
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

        }
        return null;
    }

    public static void delete(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                delete(childFiles[i]);
            }
        }
    }
}
